package com.aaa.project.controller;


import com.aaa.entity.*;
import com.aaa.project.service.IGGoodsService;
import com.aaa.project.service.IOCartService;
import com.aaa.project.service.IOOrderService;
import com.aaa.project.service.IUUserInfoService;
import com.aaa.project.util.GetBid;
import com.aaa.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author zhangxuhui
 * @since 2023-02-01
 */
@RestController
@RequestMapping("/o-cart")
public class OCartController {

    @Resource
    private IOCartService cartService;
    @Resource
    private IUUserInfoService userInfoService;
    @Resource
    private IGGoodsService goodsService;
    @Resource
    private IOOrderService orderService;
    @Resource
    private GetBid getBid;


    @GetMapping("getAll")
    public Result getAll(Principal principal, Integer curr, Integer pageSize){
        return new Result<>(cartService.getAll(principal,curr,pageSize));
    }
    @GetMapping("{id}")
    public Result deleted(@PathVariable String id){
        System.out.println("id = " + id);
        return new Result(cartService.removeById(id));
    }

    @DeleteMapping("{ids}")
    public Result deleteds(@PathVariable  String ids){
        return new Result<>(cartService.removeByIds(Collections.singleton(ids)));
    }
        /**
         * 添加购物车
         */
     @PostMapping("addCart")
    public Result addCart(OCart oCart,Principal principal){
         String name = principal.getName();
         // 根据当前的人的名字用户的id
         QueryWrapper queryWrapper = new QueryWrapper();
         queryWrapper.eq("telephone",name);
         UUserInfo uUserInfo=userInfoService.getOne(queryWrapper);
         oCart.setUid(uUserInfo.getId());
         oCart.setIsDeleted(0);
         return new Result<>(cartService.saveOrUpdate(oCart));
     }

    /**
     * 生成订单信息
     *
     * @param cartTotalPrice
     * @param selectionData
     * @return
     */
    @PostMapping("settlement/{cartTotalPrice}")
    public Result getSelectionData(@PathVariable String cartTotalPrice, @RequestBody List<SelectionData> selectionData) {
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        String uuidStr = str.replace("-", "");
        selectionData.stream().peek(selectionData1 -> {
            OOrder oOrder = new OOrder();
            oOrder.setUid(selectionData1.getUid());
            oOrder.setOrderdate(LocalDateTime.now());
            BigDecimal price = selectionData1.getPrice();
            System.out.println("price = " + price);
            Integer num = selectionData1.getNum();
            System.out.println("num = " + num);
            BigDecimal divide = price.divide(BigDecimal.valueOf(num));
            oOrder.setPrice(divide);
            oOrder.setCode(uuidStr);
            oOrder.setSend(0);
            oOrder.setCommentstatus(0);
            oOrder.setGoodsid(selectionData1.getGoodsId());
            oOrder.setNum(selectionData1.getNum());
            oOrder.setTotalmoney(NumberUtils.createBigDecimal(cartTotalPrice));
            oOrder.setAddrid(selectionData1.getAddrid());
            oOrder.setState(1);
            QueryWrapper<GGoods> gGoodsQueryWrapper = new QueryWrapper<>();
            System.out.println("gGoodsQueryWrapper = " + gGoodsQueryWrapper);
            gGoodsQueryWrapper.eq("id", selectionData1.getGoodsId());

            System.out.println("selectionData1 = " + selectionData1.getGoodsId());
            GGoods one = goodsService.getOne(gGoodsQueryWrapper);
            System.out.println("one=============================================="+goodsService.getOne(gGoodsQueryWrapper));
            System.out.println("one = " + one);
//            List<OOrder> order = cartService.getOrderByCode("006d5aa2f2324559947e7edef0f3e432");
//            System.out.println("order = =======================================" + order);
            oOrder.setBid(one.getBid());
            orderService.save(oOrder);
            UpdateWrapper<OCart> oCartQueryWrapper = new UpdateWrapper<>();
            oCartQueryWrapper.eq("id", selectionData1.getId());
            oCartQueryWrapper.set("is_deleted", 0);
            cartService.update(oCartQueryWrapper);
        }).collect(Collectors.toList());
        return new Result(uuidStr);
    }

    @GetMapping("getOderInfo")
    public Result getOderInfo( String code){
        System.out.println("=========================================================================================");
        List<OOrder> order = cartService.getOrderByCode(code);
        System.out.println("order======================================================"+order);
        return new Result(order);
    }


}

